import unittest,requests,logging

import setup_common
from setup_common import get_shared_ip,get_shared_headers,generate_url
#单项盘点
from concurrent.futures import ThreadPoolExecutor
# 配置日志记录
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(threadName)s] %(message)s')
class ManualcyclecountTest(unittest.TestCase):

    def setUp(self) -> None:
        self.ip = get_shared_ip()
        self.cycleurl = setup_common.generate_url(self.ip,"cyclecount/getgoodscyclecount")
        self.header = get_shared_headers()
        self.detailurl = generate_url(self.ip,"asn/detail")
        #self.cyclecounturl =generate_url(self.ip)
        self.json_output = []

    def test_get_asn_detail(self):
        # 获取本轮单项盘点的数据
        page = 1
        while True:
            response = requests.get(url=self.detailurl + '?asn_status=5', headers=self.header, params={"page": page}).json()
            results = response.get("results", [])
            if not results:  # 添加退出条件，当结果为空时退出循环
                break
            output_list = []
            for result in results:
                output_dict = {
                    'id': result['id'],
                    'asn_code': result['asn_code'],
                    'goods_code': result['goods_code'],
                }
                output_list.append(output_dict)
            self.json_output.extend(output_list)
            page += 1
        self.asnfinsh_list = self.json_output

    def test_get_cyclecount(self):
        self.test_get_asn_detail()
        goods_codes = [item['goods_code'] for item in self.asnfinsh_list]
        for goods_code in goods_codes:
            cycleurl = self.cycleurl + f'?goods_code={goods_code}'

            response = requests.get(url=cycleurl,headers=self.header).text
            print(response)
            # 断言goods_code是否在response中
            assert goods_code in response, f"Goods code {goods_code} 不是response里的goods_code"

    def test_post_manualcyclecount(self):
        self.test_get_cyclecount()

